package com.dingqb.ktdemo.base

import java.lang.IllegalArgumentException

fun add2(a: Int, b: Int): Int = a + b

fun add4(): String {
    return "not retun value"
}

fun add5() = "123"
fun add6(vararg v: Int) {
    for (i in v) {
        println(i)
    }
}

fun St_(): String {
    var a = 101;
    // 模板中的简单名称：
    val s1 = "a is $a"
    a = 111;
    // 模板中的任意表达式：
    val s2 = "${s1.replace("is", "was")}, but now is $a"
    println(s2);
    return "";
}

fun getStringLen(objj: Any): Int? {
    if (objj is String) {
        return objj.length;
    }
    return null;
}

fun foreach() {
    for (i in 1..10) {
        println(i);
    }
    println();
    for (i in 10 downTo 0 step 2) {
        println("$i");

    }
    println();
    for (i in 1 until 3) {
        println(i);
    }
    println(1.23e3);
    println(1234e-13);
    var floatt: Float = 12.3_123f;
    println(floatt);
    for (i in 'a'..'z') {
        println(i);
    }

}

fun convert(a: Char) {
    if (a in 'a'..'z') {
        println(a);
    } else {
        throw IllegalArgumentException("out of range");
    }
    var bb: Byte = -12;
    var cc = bb.toInt();
}

fun array() {
    var arr2 = Array(20) { a -> (a * 1) };
    for (i in 0 until arr2.size) {
        println(arr2[i]);
    }
    var aa: IntArray = intArrayOf(1, 2);
    var aa2: IntArray = IntArray(10) { a -> (a * 1) };
    for (i in aa2.indices) {
        println(i);
    }


}

fun intArra() {
    var intarr = IntArray(10, { i: Int -> (i * 2) });
    for (i in intarr.indices) {
        System.err.println(intarr[i]);
    }
    println("\n")
    for ((index, value) in intarr.withIndex()) {
        println(value)
    }
}

fun StringArr() {
    var aaa = "abc";
    for (c in aaa) {
        println(c);
    }
    val s = "runoob"
    val str = "$s'length is ${s.length}" // 求值结果为 "runoob.length is 6"
    println(str)
}

fun StringSet() {
    var sset = setOf(1, 2, 33, 2, 333, 4);
    for (i: Int in 0 until sset.size) {
        when (sset.elementAt(i)) {
            in 1..100 -> {
                println("in 1..10=" + sset.elementAt(i))
            }
            333 -> println("333==" + sset.elementAt(i));
            else -> {
                println("else ==" + sset.elementAt(i));
            }
        }
        when {
            sset.elementAt(i) in 1..2 -> println("33");
            else -> println(sset.elementAt(i));
        }
    }

    println("forfor\n\n")
    for (i in sset.indices) {
        println(sset.elementAt(i));
    }
}

fun ifelse(a: Int, b: Int): Int {
    if (b in 1..100) {
        return if (a > b) a else b;
    } else return 0

}

fun whenw(a: String, b: Any) {
    when (a) {
        "1" -> println("a==1")
        "2", "3" ->
            println("a==3")
        in "10".."20" -> {
            println("in 10..20")
        }
        !in "10".."20" -> {
            println("not in 10..20")
        }
        else -> {
            println("else==$a")
        }
    }
    when (b) {
        is String -> println("b is String ");
        else -> {
            println("b is not String $b")
        }
    }


}

fun whilew() {
    var arr = arrayOf(1, 2, 3, 4, 5, 6);
    var a = 10;
    loop@ for (i in arr.indices) {
        println(arr[i])
        if (arr[i] == 4) {
            break@loop;
        }

    }
}

//测试
fun main(args: Array<String>) {
    val aa: (Int, Int) -> Int = { a, b -> a + b }
    println(Int.MAX_VALUE);
    var ll = 1 shl 31;
    println(ll * 2);

    var arr = Array(5) { i -> { i * 2 } };
    for (i in 0..arr.size) {
        println("i==$i");
    }
    val text = """
    |多行字符串
    |菜鸟教程
    |多行字符串
    |Runoob
    """.replace("|", ">")
    // 前置空格删除了
    println(text)
    println("${text.length}");
    var v = 100;
    println("${v}");

    for (i in 1 until 3) {
        println(i);
    }

    var a1 = 10;
    var b1 = 20;
    var c1 = if (a1 > b1) a1 else b1;
    println(c1);
    var ai = IntArray(5) { i -> i * 10 };
    for (i in ai.indices) {
        println(i);
        println(ai[i]);
    }
    ai.forEach {
        if (it == 40) return@forEach
        print(it);println("--")
    }
    ai.forEach { println(it); }


}